package com.smec.remes.module.scan;

import android.app.ProgressDialog;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.smec.moblie.R;
import com.smec.moblie.SMECApplication;
import com.smec.moblie.databinding.FragmentScanBinding;
import com.smec.remes.base.SmecBaseFragment;
import com.smec.remes.module.scan.activity.SmecAllocActivity;
import com.smec.remes.module.scan.adapter.SmecOutAdapter;
import com.smec.remes.module.scan.beans.SmecMateriel;
import com.smec.remes.module.scan.beans.SmecOutboundOrder;

import java.util.ArrayList;

import static com.smec.remes.module.scan.SmecScanPresenter.GET_OUT_FAILURE;
import static com.smec.remes.module.scan.SmecScanPresenter.GET_OUT_SUCCESS;
import static com.smec.remes.module.scan.SmecScanPresenter.MAKE_SURE_FAILURE;
import static com.smec.remes.module.scan.SmecScanPresenter.MAKE_SURE_SUCCESS;

/**
 * Created by xupeizuo on 2018/3/28.
 */

public class SmecScanFragment extends SmecBaseFragment<SmecScanPresenter>{

    private FragmentScanBinding fragmentScanBinding;
    public static final String SCAN_SUCCESS="SCAN_SUCCESS";
    private ProgressDialog progressDialog=null;
    private SmecOutboundOrder smecOutboundOrder;
    private SmecOutAdapter smecOutAdapter;
    private String ckNo="";

    @Override
    protected SmecScanPresenter getPersenter() {
        return new SmecScanPresenter(getActivity());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentScanBinding= DataBindingUtil.inflate(inflater, R.layout.fragment_scan,container,false);
        smecOutboundOrder=new SmecOutboundOrder();
        fragmentScanBinding.setOutMsg(smecOutboundOrder);
        initView();
        return fragmentScanBinding.getRoot();
    }

    private void initView(){
        smecOutAdapter=new SmecOutAdapter(getContext());
        fragmentScanBinding.myRecyclerView.setAdapter(smecOutAdapter);
        fragmentScanBinding.myRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        fragmentScanBinding.makeSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                smecOutboundOrder.setOutboundOrderNoDetail(smecOutAdapter.getMakeSure());
                if(progressDialog == null){
                    progressDialog=new ProgressDialog(getContext());
                    progressDialog.setMessage("确认收货");
                    progressDialog.setCanceledOnTouchOutside(false);
                }
                progressDialog.show();
                mPresenter.makeSure(smecOutboundOrder);
            }
        });
        fragmentScanBinding.smecAlloc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), SmecAllocActivity.class);
                intent.putExtra("allocOrderNo",smecOutboundOrder.getReceiptNo());
                getActivity().startActivity(intent);
            }
        });
//        mPresenter.getOutOrderMsg("CK371805030003");
        fragmentScanBinding.smecChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(fragmentScanBinding.smecChoose.getText().equals("选择")){
                    smecOutAdapter.setChoose(true);
                    fragmentScanBinding.smecChoose.setText("取消");
                }else {
                    smecOutAdapter.setChoose(false);
                    fragmentScanBinding.smecChoose.setText("选择");
                }
            }
        });
    }


    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(SCAN_SUCCESS)
            }
    )
    public void scanSuccess(String no){
        ckNo=no;
        if(progressDialog == null){
            progressDialog=new ProgressDialog(getContext());
            progressDialog.setMessage("正在获取出库单信息");
            progressDialog.setCanceledOnTouchOutside(false);
        }
        progressDialog.show();
        mPresenter.getOutOrderMsg(ckNo);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_OUT_SUCCESS)
            }
    )
    public void getOutSuccess(SmecOutboundOrder s){
        if(progressDialog != null && progressDialog.isShowing()){
            progressDialog.dismiss();
        }
        smecOutboundOrder=s;
        fragmentScanBinding.setOutMsg(smecOutboundOrder);
        smecOutAdapter.setArrayList(smecOutboundOrder.getOutboundOrderNoDetail());
        fragmentScanBinding.getRoot().invalidate();
    }


    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_OUT_FAILURE)
            }
    )
    public void scanFailure(Object o){
        if(progressDialog != null && progressDialog.isShowing()){
            fragmentScanBinding.getRoot().postDelayed(new Runnable() {
                @Override
                public void run() {
                    progressDialog.dismiss();
                }
            },700);
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(MAKE_SURE_SUCCESS)
            }
    )
    public void makeSureSuccess(Object o){

        progressDialog.dismiss();
        if(!smecOutAdapter.isChoose()){
            smecOutboundOrder=new SmecOutboundOrder();
            fragmentScanBinding.setOutMsg(smecOutboundOrder);
            smecOutAdapter.setArrayList(new ArrayList<SmecMateriel>());
            fragmentScanBinding.invalidateAll();
        }else {
            smecOutAdapter.removeChoose();
            if(smecOutAdapter.getArrayList().size() == 0){
                smecOutboundOrder=new SmecOutboundOrder();
                fragmentScanBinding.setOutMsg(smecOutboundOrder);
                fragmentScanBinding.invalidateAll();
            }
        }
        mPresenter.getOutOrderMsg(ckNo);
        Toast.makeText(SMECApplication.getContext(),"确认收货成功",Toast.LENGTH_SHORT).show();
    }


    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(MAKE_SURE_FAILURE)
            }
    )
    public void makeSureFailure(Object o){
        progressDialog.dismiss();
    }

    public static SmecScanFragment newInstance() {
        Bundle args = new Bundle();
        SmecScanFragment fragment = new SmecScanFragment();
        fragment.setArguments(args);
        return fragment;
    }
}
